-- TODO: Comment 

PermanentGameelement = Gameelement:new{
	hp = 100,
	hpMax = 100,
	hpRegenerationRate = 1,
	mana = 100,
	manaRegenerationRate = 1,
	manaMax = 100,
	movementSpeed = 0,
	passable = false,
	expWhenKilled = 0,
	destroyer = "default",
	attack = {
		meleRange = 0, 
		meleDamage = 0,
		meleSpeed = 0,
		
		distanceDamage = 0,
		distanceSpeed = 0
	},
	skills = {}
} 

function PermanentGameelement:getDamage(damage, causer)
	self.hp = self.hp - damage
	self.destroyer = causer
	-- bL00Dy
	if self.blood then
		local i_width = 24
		local i_height = 32
		local  anim = newAnimation(img_blood, i_width, i_height, 0.5, 0)
		anim:setMode("once")
		table.insert(once_animations,{anim, self.center[1] - i_width/2, self.center[2] - i_height/2 })
	end
	-- over head text	
	graphic.newTexteffect(self.center[1], self.center[2], damage, 1, 100)
	-- autsch sound!
	love.audio.rewind(meow)
	love.audio.play(meow)
end

function PermanentGameelement:regenerateHp()
	if 		self.hpRegTime == nil or self.hpRegTime < clock.second then
			self.hpRegTime = clock.second + 1

	elseif	self.hpRegTime == clock.second then
			self:getHp(self.hpRegenerationRate)
			self.hpRegTime = clock.second + 1
	end
	
	if self.hpRegTime > 59 then self.hpRegTime = 0 end
end

function PermanentGameelement:getMana(amount)
	if (mana + amount < manaMax) then
		self.mana = self.mana + amount
	else
		mana = manaMax
	end
end

function PermanentGameelement:getHp(amount)
	if (self.hp + amount < self.hpMax) then
		self.hp = self.hp + amount
	else
		hp = hpMax
	end
end

function PermanentGameelement:getExp(amount)
	self.exp = self.exp + amount
end

function PermanentGameelement:giveExp(amount)
	self.destroyer:getExp(self.expWhenKilled)
end

function PermanentGameelement:regenerateMana()
	if 		self.manaRegTime == nil or self.manaRegTime < clock.second then
			self.manaRegTime = clock.second + 1

	elseif	self.manaRegTime == clock.second then
			self:getMana(self.manaRegenerationRate)
			self.manaRegTime = clock.second + 1
	end
	
	if self.manaRegTime > 59 then self.manaRegTime = 0 end
end

function PermanentGameelement:meleAttack(target) -- an attack which hits enemies, collected in the table targets, and deal them damage
	
	if self.direction == 1 then  --north		
	  	self.attack.x = self.x 
	  	self.attack.y = self.y - (self.attack.meleRange)

	  	elseif self.direction == 5 then --northeast
	  	self.attack.x = self.x + (self.width/2)  --TODO: calibrate the final range
	  	self.attack.y = self.y

		  elseif self.direction == 2 then	--east
	  	self.attack.x = self.x
	  	self.attack.y = self.y + (self.height/2)								

	  	elseif self.direction == 6 then --southeast
	  	self.attack.x = self.x - (self.attack.meleRange/2)
	  	self.attack.y = self.y						

	 	 elseif self.direction == 3 then --south
	  	self.attack.x = self.x + (self.width/2)
	  	self.attack.y = self.y - (self.attack.meleRange/2)						

		  elseif self.direction == 7 then --southwest
	  	self.attack.x = self.x + (self.width/2)
	  	self.attack.y = self.y + (self.height/2)						

		 elseif self.direction == 4 then --west
	  	self.attack.x = self.x - (self.attack.meleRange/2)
	  	self.attack.y = self.y - (self.height/2)						

		  elseif self.direction == 8 then --northwest
	  	self.attack.x = self.x - (self.attack.meleRange/2)
	  	self.attack.y = self.y - (self.attack.meleRange/2)						
	  end

	  if #target > 1 then
	  	for i = 1, #target do
	  		if 	compare(self.attack.x, self.attack.y, self.attack.meleRange, self.attack.meleRange, target[i].x, target[i].y, target[i].width, target[i].height) == true
	  		then
	  			self:dealDamage(target[i], self.attack.meleDamage) 
	  			collectgarbage()
	  			
	  			
	  		end
	  	end	
	  end
  
end

function PermanentGameelement:statusCheck()
	if self.hp <= 0 and self.currentState.name ~= "dead" then self:changeState(self.stateList.dead) end
 	if self.mana < self.manaMax then self:regenerateHp()end
 	if self.hp < self.hpMax then self:regenerateHp()end
end

function PermanentGameelement:dropItem(droppedItem)
	o = ItemGameelement:new{
		x = self.x,  -- add range
		y = self.y,
		item = Item:new(droppedItem),
		--image = droppedItem.icon, -- nmaybe add special ingame image
	}
	table.insert(gameelements, o)
end
dofile('character.lua')
